<meta name="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
var currentTooltip;//当前弹出的气泡对话框
var currentTooltipSchedule;//当前弹出对话框的课程节点

$(function(){
	var now = new Date();
	var curDate = now.getFullYear()+"-"+(now.getMonth()+1) + "-" + now.getDate();
	ajaxSearchScheduleList(curDate);
	
	$(document).on("mouseover", ".showScheduleInfo", function(){
		currentTooltipSchedule = $(this).parents(".kcbInfo");
		var content = $(".hideScheduleInfo", $(this).parent()).html();
		if(currentTooltip){
			currentTooltip.close();
		}
		currentTooltip = tooltip($(this).get(0), content);
	});
});
//--查询班级的全部课表
function ajaxSearchScheduleList(curDate){
	var classId = "${classId}";	
	var url = "yueyogaAdmin/ajaxGetScheduleList.jhtml";
	$.post(url, 
		{
			"clsId":classId,
			"curDate":curDate
		}, function(json){
			var result = eval("("+json+")");
			var success = result.success;
			if(success){
				var curDate = result.curDate;
				var items = result.items;
				//填充表表格中
				$("#scheduleList table tr:not(:eq(0))").remove();
				$.each(items, function(index,item){
					var kcrq = item.kcrq +"";
					var kcIds  = new Array();
					var ksjss  = new Array();
					var kclss  = new Array();
					var kczjs  = new Array();
					var kccds  = new Array();
					var sfyxs  = new Array();
					var kcxhs  = new Array();
					var kcmcs  = new Array();
					var cxrss  = new Array();
					var len = 0;
					if(item.kcbh){
						kcIds = item.kcbh.split("#");
					}
					if(item.ksjs){
						ksjss = item.ksjs.split("#");
						len = ksjss.length;
					}
					if(item.kcls){
						kclss = item.kcls.split("#");
					}
					if(item.kczj){
						kczjs = item.kczj.split("#");
					}
					if(item.kccd){
						kccds = item.kccd.split("#");
					}
					if(item.sfyx){
						sfyxs = item.sfyx.split("#");
					}
					if(item.kcxh){
						kcxhs = item.kcxh.split("#");
					}
					if(item.kcmc){
						kcmcs = item.kcmc.split("#");
					}
					if(item.zxws){
						zxwss = item.zxws.split("#");
					}
					
					var sjd = "";
					for(var k=0;k<len;k++){
						var kId  = kcIds[k];
						var ksjs = ksjss[k];
						var kcls = kclss[k];
						if(!kcls||kcls=='null'){
							kcls = '';
						}
						var kczj = kczjs[k];
						if(!kczj||kczj=='null'){
							kczj = '';
						}
						var kccd = kccds[k];
						if(!kccd||kccd=='null'){
							kccd = '';
						}
						var sfyx = sfyxs[k];
						var kcxh = kcxhs[k];
						var kcmc = kcmcs[k];
						var zxws = zxwss[k];
						var sjdTemp = "<div class='kcbInfo'><div class='showScheduleInfo'><span id='span_sjd_"+kId+"_"+kcxh+"'>";
						if(ksjs&&ksjs!='null'){
							sjdTemp +="<a href='#'>" +ksjs +"</a></span>&nbsp;&nbsp;&nbsp;&nbsp;</div>";
							sjdTemp +="<div class='hideScheduleInfo' style='display:none;'>课程："+kcmc+"<br>席位："+zxws+"<br>老师："+kcls+"<br>助教：" +kczj +"<br>场地：" + kccd +"<br>";
							if(sfyx&&sfyx=='1'){
								sjdTemp += "<span id='sckc_span'><a href='javascript:delScheduleById("+kcxh+","+sfyx+","+kId+",\""+kcrq+"\")'>删除课程</a></span><span id='qxyd_span'><a href='javascript:cancelReverse(" + kcxh + ")'>取消预订</a></span><span id='kcdz_span'><a href='javascript:showReservationsList("+kcxh+",\""+kcrq+"\")'>预订详情</a></span></div>";
							}else{
								sjdTemp += "<span id='sckc_span'><a href='javascript:delScheduleById("+kcxh+","+sfyx+","+kId+",\""+kcrq+"\")'>删除课程</a></span><span id='kqdz_span'><a href='javascript:scheduleDz("+kcxh+","+kId+",\""+kcrq+"\")'>开启预订</a></span></div>";
							}
						}
						sjd += sjdTemp+"</div>";
					}
					var str = "<tr><td>"+item.kcrq+"</td><td>星期"+item.kcxq+"</td><td>"+sjd+"</td><td><a href='javascript:showAddSchedule(\""+kcrq+"\")'>添加</a></td></tr>";
					$("#scheduleList table").append(str);
				});
				$("#li-prev .prev").attr("href","javascript:ajaxSearchScheduleList('"+result.prvDate+"')");
				$("#li-next .next").attr("href","javascript:ajaxSearchScheduleList('"+result.nexDate+"')");
			}
		});
}

function delScheduleById(kcxh,sfyd,scheId,kcrq){
	var str ="确定要删除吗?";
	if(sfyd==1){
		str ="可能存在已经预定的座位，" + str;
	}
	confirm(str, function() {
		this.close();
	var url = "yueyogaAdmin/ajaxDelScheduleById.jhtml";
	$.post(url,{kcxh:kcxh,scheduleId:scheId,curDate:kcrq},
			function(resp){
		var data = eval("("+resp+")");
		if(data.success){
			$("#span_sjd_"+scheId+"_"+kcxh).remove();
		}else{
			error(data.msg);
		}
	});
	});
}
//--根据当前日期和班级编号添加课程
function addSchedule(curDate,clsId){
	var url = "yueyogaAdmin/ajaxAddSchedule.jhtml";
	$.post(url,{curDate:curDate,clsId:clsId},
			function(resp){
		var data = eval("("+resp+")");
		if(data.success){
			window.location.reload();
		}else{
			error(data.msg);
		}
	});
}
//--开启订座
function scheduleDz(kcxh,scheduleId,kcrq){
	var url = "yueyogaAdmin/ajaxReservations.jhtml";
	$.post(url,{kcxh:kcxh,scheduleId:scheduleId,curDate:kcrq},
			function(resp){
		var data = eval("("+resp+")");
		if(data.success){
			var d = alert(data.msg);
			setTimeout(function(){
				d.close().remove();
				window.location.reload();
			},1000);
		}else{
			error(data.msg);
		}
	});
}
//--开启订座
function scheduleDzSubmit(){
	var seatNumber = $("#seatNumber").val();
	if(!seatNumber){
		alert("请填写预定数量!");
		showPanel();
		return;
	}
	var scheduleId = $("#scheduleId").val();
	var kcrq = $("#kcrq").val();
	var kcxh = $("#kcxh").val();
	var url = "yueyogaAdmin/ajaxReservations.jhtml";
	$.post(url,{kcxh:kcxh,scheduleId:scheduleId,curDate:kcrq,seatNumber:seatNumber},
			function(resp){
		var data = eval("("+resp+")");
		if(data.success){
			window.location.reload();
		}else{
			error(data.msg);
		}
	});
	$("#seatNumber").val("");
}
function showPanel(){
	$("#ydSeat").show();
}
function hidePanel(){
	$("#ydSeat").hide();
}
function closeReservationPanel(){
	$("#ydList").hide();
}
function showReservationPanel(){
	$("#ydList").show();
}
//--查询课程座位使用信息
function showReservationsList(kcxh,curDate){
	showReservationPanel();
	var url = "yueyogaAdmin/ajaxGetReservations.jhtml";
	$.post(url,{curDate:curDate,ydId:kcxh},
		function(resp){
		var data = eval("("+resp+")");
		if(data.success){
			var items = data.items;
			var len = items.length;
 			//填充表表格中
			$("#ydList table tr:not(:eq(0))").remove();
 			var str = "<tr>";
			$.each(items, function(index,item){
				var seat = item.seatNo;
				var memberId = item.memberId;
				var memberName = item.memberName;
				var id = item.id;
				str = str + "<td id='yd_"+id+"' class='schedule-td'>";
				if(memberId){
					str = str + "<a class='seat-member' id='ydr_"+id+"' href='javascript:cancelReservation("+id+","+seat+")'>"+memberName+"</a></td>";
				}else{
					str = str + "<a id='ydr_"+id+"' href='javascript:memberReservation("+id+","+seat+")'>"+ seat +"</a></td>";
				}
				if((index+1)%10==0){
					str=  str + "</tr><tr>";
				}
			});
			var kwNum = 10 -(len%10);
			if(kwNum <10){
				for(var i=0;i<kwNum;i++){
					str= str + "<td>-</td>";
				}
			}
			str = str + "</tr>";
			$("#ydList table").append(str);
		}else{
			error(data.msg||"查询失败!");
		}
	});
}

//取消预订
function cancelReverse(kcxh) {
	console.log(kcxh);
	confirm("确定取消吗？", function() {
		$.post(
			"${basePath}/yueyogaAdmin/ajaxCancelReserve.jhtml",
			{"scheduleId": kcxh},
			function(response) {
				var res = eval("(" + response + ")");
				if (res.success) {
					alert(res.msg);
					window.location.reload();
				} else {
					alert(res.msg);
				}
			}
		);
	});
}

function memberReservation(ydId,seatNo){
	showMemberReservation();
	$("#ydId").val(ydId);
	$("#ydSeat").val(seatNo);
}
function closeMemberReservation(){
	$("#ydPersonSeat").hide();
}

function showMemberReservation(){
	$("#ydPersonSeat").show();
}
function memberReservationSubmit(){
	closeMemberReservation();
	var ydId = $("#ydId").val();
	var seat = $("#ydSeat").val();
	var memberId = $("#selMembers").val();
	var mName = $("#selMembers option:selected").text();
	if(!memberId){
		alert("请选择订座会员!");
		showMemberReservation();
		return;
	}
	var memberNames = mName.split('-');
	if(memberNames.length > 1){
		mName = memberNames[1];
	}
	var url = "yueyogaAdmin/ajaxMemberReservation.jhtml";
	$.post(url,{ydId:ydId,memberId:memberId},
		function(resp){
		var data = eval("("+resp+")");
		if(data.success){
			var tstr = "<a class='seat-member' id='ydr_"+ydId+"' href='javascript:cancelReservation("+ydId+","+seat+")'>"+mName+"</a>";
			$("#yd_"+ydId).html(tstr);
		}else{
			error(data.msg);
		}
 	});
	$("#selMembers").val(" ");
}

//--取消订座
function cancelReservation(id,seat){
	closeReservationPanel();
	if(id){
	confirm("确定取消该会员的订座吗?", function() {
		this.close();
		var url = "yueyogaAdmin/ajaxCancelReservation.jhtml";
		var d = wait("正在取消，请稍候...");
		$.post(url, {reservateId:id}, function(msg) {
			var json = eval("("+msg+")");
			d.close();
			if (json.success) {
				showReservationPanel();
				var strtemp = "<a id='ydr_"+id+"' href='javascript:memberReservation("+id+")'>"+ seat +"</a>";
				$("#yd_"+id).html(strtemp);
			} else {
				error(json.msg || "取消失败!");
			}
		});
	});
	}else{
		error("未能获取座位序号!");
	}
}

function showScheduleDetailPanel(){
	$("#addScheduleDetailPanel").show();
}

function closeScheduleDetailPanel(){
	$("#addScheduleDetailPanel").hide();
}


function showAddSchedule(kcrq){
	showScheduleDetailPanel();
	$("#hidKcrq").val(kcrq);
}

function addScheduleDetail(){
	closeScheduleDetailPanel();
	var scheduleTime = $("#hidKcrq").val();
	var classId = "${classId}";	
	var startTime = $("#kcStartTime").val();
	var endTime = $("#kcEndTime").val();
	var teacherId = $("#selTeachers").val();
	var assistantId = $("#selAssistants").val();
    var placeId = $("#selPlaces").val();
    var scheduleName = $("#scheduleName").val();
    var seatNumber = $("#seatNumber").val();
	
    if(!startTime || !endTime){
		alert("开始时间和结束时间不能为空!");
		showScheduleDetailPanel();
		return;
	}
	
	if(isNaN(seatNumber)){
		alert("请输入规范席位数!");
		showScheduleDetailPanel();
		return;
	}
	
	if($.trim(seatNumber)==''){
		alert("席位数不能为空!");
		showScheduleDetailPanel();
		return;
	}
	
	var url = "yueyogaAdmin/ajaxAddScheduleDetail.jhtml";
	$.post(url,{classId:classId,scheduleTime:scheduleTime,startTime:startTime,endTime:endTime,teacherId:teacherId,
		assistantId:assistantId,placeId:placeId,scheduleName:scheduleName,seatNumber:seatNumber},
		function(resp){
		var data = eval("("+resp+")");
		if(data.success){
			window.location.reload();
		}else{
			error(data.msg);
		}
	});
	$("#hidKcrq").val("");
	$("#kcStartTime").val("");
	$("#kcEndTime").val("");
	$("#selTeachers").val("");
	$("#selAssistants").val("");
    $("#selPlaces").val("");
    $("#scheduleName").val("");
    $("#seatNumber").val("");
}
$(function(){
	$("#kcstTime").datetimepicker({
		language: "zh-CN",
		pickDate: false,
		useStrict: true
	});

	$("#kcetTime").datetimepicker({
		language: "zh-CN",
		pickDate: false,
		useStrict: true
	});
	});
</script>
<div class="panel-heading panel-result-title">
	<h3 class="panel-title">详细课表</h3>
</div>
<div id="scheduleList" class="tab-pane">
	<table class="table table-bordered table-hover table-condensed tb-list-default tb" style="text-align: center;">
		<tr>
			<td class="tb-time">课表日期</td>
			<td class="tb-time">星期</td>
			<td>课程时间</td>
			<td class="tb-time">操作</td>
		</tr>
	</table>
	<div class="schedulePageBar">
		<ul class="pagination">
			<li id="li-prev"><a class="prev" href="javascript:void(0)">上一页</a></li>
			<li id="li-next"><a class="next" href="javascript:void(0)">下一页</a></li>
		</ul>
	</div>
</div>

<!-- <div id="ydSeat" class="panel-dialog-box panel-radius-all" style="width:300px;height:100px;display:none;left:40% !important;top:40%  !important;">
	<div class="panel-heading panel-result-title">
			<h3 class="panel-title">预定课程</h3>
			<br> <br>
			<div>&nbsp;</div>
			<label for="seatNumber" style="float:left;">预定数量：</label>
					<div class="col-sm-6">
						<input type="text" required class="form-control" style="width:150px;" id ="seatNumber"/>
					</div>
	<br> <br><br>
	<input id="kcrq" type="hidden"/>
	<input id="scheduleId" type="hidden"/>
	<input id="kcxh" type="hidden"/>
	<div style="margin-right:-17px;" class="clear">
	<button class="btn btn-default btn-ade-default" type="button"  onclick="scheduleDzSubmit()">预定</button>
	<button type="button"  class="btn btn-default btn-ade-default" onclick="hidePanel()">取消</button>
	</div>
	</div>
</div> -->

<div id="ydList" class="panel-dialog-box panel-radius-all" style="width:800px;height:450px;display:none;left:25% !important;top:15%  !important;overflow-y:auto;z-index:8;">
	<table class="table table-bordered table-hover table-condensed tb" style="text-align: center;">
		<tr><td colspan="10">课程预定详情<a style="float:right;margin-right:5px;" href="javascript:closeReservationPanel()">&nbsp;×&nbsp;</a></td></tr>
	</table>
</div>

<div id="ydPersonSeat" class="panel-common-small panel-dialog-box panel-radius-all panel-common-small" style="width:300px;min-height:170px !important;display:none;left:40% !important;top:40%  !important;z-index:10;">
	<div class="panel-heading panel-result-title">
			<h3 class="panel-title">预定座位</h3>
			<br> <br>
			<div>&nbsp;</div>
			<label for="selMembers" class="cls-lb clear">订座会员：</label>
			<div class="col-sm-8"  class='panel-radius-all' style="float:right">
						<select id="selMembers" class="selectpicker show-tick" data-width="100%">								
								<option value=""> </option>
								<!-- #for(ClassMemberModel mm : allMembers) -->
								<option value="${mm.id}">
								<!-- #if(mm.memberCode != null && mm.memberCode != "") -->
								  ${mm.memberCode}-
								<!-- #end --> ${mm.name}
								</option>
								<!-- #end -->
							</select>
			</div>
			<br> <br><br>
	<input id="ydId" type="hidden"/>
	<input id="ydSeat" type="hidden"/>
	<div style="margin-right:-24px;" class="clear">
	<button class="btn btn-default btn-ade-default" type="button"  onclick="memberReservationSubmit()">预定</button>
	<button type="button"  class="btn btn-default btn-ade-default" onclick="closeMemberReservation()">取消</button>
	</div>
	</div>
</div>

<div id="addScheduleDetailPanel" class="panel-dialog-box panel-radius-all panel-common-small" style="width:350px;min-height:200px;display:none;left:38% !important;top:40%  !important; z-index: 10;">
	<div class="panel-heading panel-result-title">
			<h3 class="panel-title">预定座位</h3>
			<div id="kcyd-cdts">
						<label for="inputStartDate" class="cls-lb clear">课程名称</label>
						<div class='col-sm-9'>
								 <input class="form-control" type="text" name="scheduleName" id="scheduleName"/>
					    </div>
					    <br /> <br /> 
					    <label for="inputStartDate" class="cls-lb clear">席位数</label>
						<div class='col-sm-9'>
								  <input class="form-control" type="text" name="seatNumber" id="seatNumber"/>
					    </div>
					    <br /> <br /> 
						<label for="inputStartDate" class="cls-lb clear">开始时间</label>
						<div class='col-sm-9'>
								 <div class='input-group date' id='kcstTime' data-date-format="HH:mm">
								     <input id="kcStartTime" type='text' class="form-control" />
								       <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span>
								       </span>
								  </div>
					    </div>

						<br /> <br /> <label for="inputendDate" class="cls-lb clear">结束时间</label>
						<div class='col-sm-9'>
								                <div class='input-group date' id='kcetTime' data-date-format="HH:mm">
								                    <input id="kcEndTime" type='text' class="form-control" />
								                    <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span>
								                    </span>
								                </div>
								            </div>
						<label for="inputCoach0" class="cls-lb clear">课程老师</label>
						<div class="col-sm-9">
							<select id="selTeachers" data-live-search="true" class="selectpicker show-tick" data-width="100%" name="cheduleCoach">
												<option value=""> </option>
												<!-- #foreach(Coach coach : coachList) -->
													<option value="${coach.coachId}">${coach.name}</option>
												<!-- #end -->
						</select>
						</div>
						<br /> <br />
						<label for="inputCoach1" class="cls-lb clear">课程助教</label>
						<div class="col-sm-9" class="panel-radius-all">
							<select id="selAssistants" data-live-search="true" class="selectpicker show-tick" data-width="100%" name="cheduleCoachHelp">
												<option value=""> </option>
												<!-- #foreach(Coach coach : helpCoachList) -->
													<option value="${coach.coachId}">${coach.name}</option>
												<!-- #end -->
							</select>
						</div>
						<label for="inputPlacement" class="cls-lb clear">课程场地</label>
						<div class="col-sm-9">
							<select id="selPlaces" data-live-search="true" class="selectpicker show-tick" data-width="100%" name="chedulePlacement">
												<option value=""> </option>
												<!-- #foreach(Place place : placeList) -->
													<option value="${place.id}">${place.name}</option>
							<!-- #end -->
				</select>
	</div>
	<input id="hidKcrq" type="hidden"/>
	<div style="margin-right:-17px;" class="clear">
	<button class="btn btn-default btn-ade-default" type="button"  onclick="addScheduleDetail()">预定</button>
	<button type="button"  class="btn btn-default btn-ade-default" onclick="closeScheduleDetailPanel()">取消</button>
	</div>
	</div>
	</div>
</div>
